package knowledge

import (
	"sync"
)

// SearchResult represents a search result with score
type SearchResult struct {
	Document *Document
	Score    float64
}

// IndexStrategy defines the interface for vector indexing and search
type IndexStrategy interface {
	// Index adds a document to the index
	Index(doc *Document) error

	// Search performs a similarity search
	Search(embedding []float32, limit int) []*SearchResult

	// Delete removes a document from the index
	Delete(id string) error

	// Clear removes all documents from the index
	Clear() error
}

// FaissIndex implements IndexStrategy using FAISS
type FaissIndex struct {
	sync.RWMutex
	index     interface{} // TODO: Replace with actual FAISS index type
	documents map[string]*Document
	dimension int
}

// NewFaissIndex creates a new FAISS index
func NewFaissIndex() *FaissIndex {
	return &FaissIndex{
		documents: make(map[string]*Document),
		dimension: 1536, // Default OpenAI embedding dimension
	}
}

// Index implements IndexStrategy
func (f *FaissIndex) Index(doc *Document) error {
	f.Lock()
	defer f.Unlock()
	f.documents[doc.ID] = doc
	return nil
}

// Search implements IndexStrategy
func (f *FaissIndex) Search(embedding []float32, limit int) []*SearchResult {
	f.RLock()
	defer f.RUnlock()
	// TODO: Implement actual FAISS search
	return []*SearchResult{}
}

// Delete implements IndexStrategy
func (f *FaissIndex) Delete(id string) error {
	f.Lock()
	defer f.Unlock()
	delete(f.documents, id)
	return nil
}

// Clear implements IndexStrategy
func (f *FaissIndex) Clear() error {
	f.Lock()
	defer f.Unlock()
	f.documents = make(map[string]*Document)
	return nil
}
